/*
 * @(#)SmartPortalAction.java $version 2011. 10. 14
 * 
 * Copyright 2011 NHN Corp. All rights Reserved.
 * NHN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.hangame.mbl.action;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.hangame.common.actions.HangameBaseAction;
import com.hangame.common.security.user.HangameUser;

import com.nhncorp.lucy.common.data.DataHandler;
import com.nhncorp.lucy.common.data.DataHandlerFactory;

// TODO: Auto-generated Javadoc
/**
 * 공통 부모 클래스.
 * 
 * @author khpark
 */
public class SmartPortalAction extends HangameBaseAction {
	
	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 8601696622135395606L;

	/** The Constant CONFIG. */
	static final DataHandler CONFIG = DataHandlerFactory.getDataHandler();
	
	/** The log. */
	private static Log log = LogFactory.getLog(SmartPortalAction.class);

	/** The action name. */
	protected String actionName;
	
	/** The space name. */
	protected String spaceName;

	/* (non-Javadoc)
	 * @see com.nhncorp.hancore.action.HancoreBaseAction#init()
	 */
	@Override
	public String init() throws Exception {
		setBaseInfo();
		return super.init();
	}

	/* (non-Javadoc)
	 * @see com.nhncorp.lucy.web.actions.NoneContinuableActionSupport#execute()
	 */
	@Override
	public String execute() throws Exception {
		return SUCCESS;
	}

	/**
	 * Sets the base info.
	 */
	protected void setBaseInfo() {
		String uri = request.getRequestURI();
		String subUri = StringUtils.substringAfterLast(uri, "/");
		actionName = StringUtils.substringBeforeLast(subUri, ".");
		subUri = StringUtils.substringBeforeLast(uri, "/");
		spaceName = StringUtils.substringAfterLast(subUri, "/");
		setAttribute("actionName", actionName);
		setAttribute("spaceName", spaceName);
		setAttribute("IS_LOGIN", String.valueOf(getUser().isLoggedin()));
		setAttribute("USER", getUser());
		try {
			setAttribute("URL_CURRENT", URLEncoder.encode(request.getRequestURL().toString(), "utf-8"));
		} catch (UnsupportedEncodingException e) {
			log.error(e, e);
		}
		try {
			setAttribute("URL_CURRENT_PARAMS", URLEncoder.encode(request.getRequestURL() + "?" + request.getQueryString(), "utf-8"));
		} catch (UnsupportedEncodingException e) {
			log.error(e, e);
		}
	}

	/**
	 * Checks if is login.
	 * 
	 * @return true, if is login
	 */
	protected boolean isLogin() {
		HangameUser user = (HangameUser) getUser();
		if (user != null) {
			return user.isLoggedin();
		} else {
			return false;
		}
	}

}
